package com.yjhb.android.sbz.adapter;

import android.content.Context;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.Timer;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建者 zx
 * 创建时间 2020/11/25 11:45
 */
public class TimerAdapter extends BaseRecyclerAdapter<TimerAdapter.Holder, Timer> {
    private final String TAG = "StepSettingAdapter";

    public TimerAdapter(Context context) {
        super(context);
    }


    @Override
    public void onBindViewHolder(@NonNull Holder holder, int i) {
        try {
            final Timer timer = data.get(i);
            final int index = i;
            holder.timeTv.setText(timer.getStartTime());
            holder.dateTv.setText("设置日期 " + timer.getStartDay());
            if (timer.getHours() < 1) {
                holder.time_length_tv.setVisibility(View.GONE);
            } else {
                holder.time_length_tv.setVisibility(View.VISIBLE);
            }
            holder.time_length_tv.setText("置顶时长:" + timer.getHours() + "小时");
            holder.typeTv.setText(timer.getIsLoop() == 1 ? "每天" : "一次");
            holder.switchOnOff.setChecked(timer.getState() == 0);
            holder.switchOnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (itemClickListener != null) {
                        itemClickListener.onViewClick(buttonView, "switch", index, timer);
                    }
                }
            });
        } catch (Exception e) {
        }
    }


    @Override
    protected Holder getHolder(View v) {
        return new Holder(v);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_timer;
    }


    class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.time_tv)
        TextView timeTv;
        @BindView(R.id.date_tv)
        TextView dateTv;
        @BindView(R.id.type_tv)
        TextView typeTv;
        @BindView(R.id.switch_on_off)
        Switch switchOnOff;
        @BindView(R.id.time_length_tv)
        TextView time_length_tv;

        public Holder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
